package com.cpms.framework.common.core.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serial;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 *  基础类型， 可以当作枚举用，避免枚举序列化问题
 * @author gulang
 * @date 2021/10/17 17:17
 */
public abstract class BaseType implements  Serializable {
	private static final Logger log = LoggerFactory.getLogger(BaseType.class);
    @Serial
	private static final long serialVersionUID = -199712849987856225L;

	/**
	 *  唯一编码
	 */
	private String code;
	/**
	 * 编码描述
	 */
	private String description;
	/**
	 * 扩展信息
	 */
	private String extend;

	protected BaseType(){}
	protected BaseType(String code, String description,String extend) {
		this.code = code;
		this.description = description;
		this.extend = extend;
	}

	@SuppressWarnings("unchecked")
	public static <T extends BaseType> List<T> getAll(Class<T> clazz){
		List<T> list = new ArrayList<T>();
		try{
			Field[] fieldlist = clazz.getDeclaredFields();
			for(Field field : fieldlist){
				if(field.getType().isAssignableFrom(clazz)){
					list.add((T)field.get(null));
				}
			}
		}catch(Exception e){
			log.error("BaseType->getAll() Exception",e);
		}
		return list;
	}
	public static <T extends BaseType> T getTypeByCode(Class<T> clazz, String code){
		try{
			List<T> list =  BaseType.getAll(clazz);
			for(T t: list){
				if(t.getCode().equals(code) ){
					return t;
				}
			}
		}catch(Exception e){
			log.error("BaseType->getTypeByCode() Exception",e);
		}
		return null;
	}
	public static <T extends BaseType> T getTypeByDesc(Class<T> clazz, String desc){
		try{
			List<T> list =  BaseType.getAll(clazz);
			for(T t: list){
				if(t.getDescription().equals(desc) ){
					return t;
				}
			}
		}catch(Exception e){
			log.error("BaseType->getTypeByDesc() Exception",e);
		}
		return null;
	}

	public static <T extends BaseType> T getTypeByExtend(Class<T> clazz, String extend){
		try{
			List<T> list =  BaseType.getAll(clazz);
			for(T t: list){
				if(t.getExtend().equals(extend) ){
					return t;
				}
			}
		}catch(Exception e){
			log.error("BaseType->getTypeByExtend() Exception",e);
		}
		return null;
	}

	public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

	public String getExtend() {
		return extend;
	}

	public void setExtend(String extend) {
		this.extend = extend;
	}

	/**
	 *  重写 hashCode，可以比较自定义对象
	 */
    @Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((code == null) ? 0 : code.hashCode());
		result = prime * result + ((extend == null) ? 0 : extend.hashCode());
		return result;
	}

	/**
	 *  重写 equals，可以比较自定义对象
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj){
			return true;
		}
		if (obj == null){
			return false;
		}
		if (getClass() != obj.getClass()){
			return false;
		}
		BaseType other = (BaseType) obj;
		if (description == null) {
			if (other.description != null){
				return false;
			}
		} else if (!description.equals(other.description)){
			return false;
		}
		if (code == null) {
			if (other.code != null){
				return false;
			}
		} else if (!code.equals(other.code)){
			return false;
		}
		if (extend == null) {
			if (other.extend != null){
				return false;
			}
		} else if (!extend.equals(other.extend)){
			return false;
		}
		return true;
	}

	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public String toString() {
		return "BaseType{" +
				"code='" + code + '\'' +
				", description='" + description + '\'' +
				", extend='" + extend + '\'' +
				'}';
	}
}
